global class preRegistrationClass {
    Webservice static string createPortalUser(ID contactId){
        Contact contactObject = [Select Id, FirstName, Email, Account.Name, Account.MyAFZA_Account_ID__c from Contact where id=:contactId];
       
       try
         {
        //Now Creating the Portal  Community User...
        User userObject = new User();
                
        userObject.username = contactObject.account.MyAFZA_Account_ID__c.trim().toLowerCase() + '@myafza.ae';
        System.debug('The Username : '+ userObject.username);
        userObject.email = contactObject.Email;
        userObject.lastName = contactObject.FirstName;
        userObject.firstName = contactObject.FirstName;
        String alias = userObject.username;
        if(alias.length() > 8) {
           alias = alias.substring(0, 8);
        }
        userObject.alias = alias;
        userObject.CommunityNickname = alias + Math.random();
        userObject.languagelocalekey = 'en_US';
        userObject.localesidkey = 'en_US';
        userObject.emailEncodingKey = 'UTF-8';
        userObject.timeZoneSidKey = 'Asia/Dubai';
        userObject.ContactId = contactObject.Id;
        userObject.profileId = [SELECT Id FROM profile WHERE name='AFZA Mobile APP Users'].Id;
                
        //For setting the DML Opertaions...
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.EmailHeader.triggerUserEmail = true;
                
        userObject.setOptions(dmo);
                
        //insert userObject;
        Database.SaveResult userList = Database.insert(userObject,dmo );    
        system.debug('## userList =>' + userList);
        if (userList.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        //System.debug('Successfully inserted account. Account ID: ' + sr.getId());
            return 'Success';
        } else {
            //String var = userList.getErrors();
            return string.valueOf(userList.getErrors());    
        }
        } catch (Exception err)
        {
             //return err.getMessage(); 
             return 'Error => '+ err.getMessage();
        }
    }
}